package cn.tedu.csmallproducts.controller;

import cn.tedu.csmallproducts.pojo.dto.AlbumUpdateDTO;
import cn.tedu.csmallproducts.pojo.dto.AttributeTemplateDTO;
import cn.tedu.csmallproducts.pojo.dto.AttributeTemplateUpdateDTO;
import cn.tedu.csmallproducts.pojo.vo.AlbumStandardVO;
import cn.tedu.csmallproducts.pojo.vo.AttributeTemplateListItemVO;
import cn.tedu.csmallproducts.pojo.vo.AttributeTemplateStandardVO;
import cn.tedu.csmallproducts.service.IAttributeTemService;
import cn.tedu.csmallproducts.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/AttributeTemplate")
@Api(tags = "02.属性模板管理模块")
public class AttributeTemplateController {

    @Autowired
    IAttributeTemService service;

    @ApiOperation("添加属性模板")
    @PostMapping("/add-new")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Valid AttributeTemplateDTO attributeTemplateDTO){
        log.debug("开始处理【添加属性模板业务】，模板信息：{}",attributeTemplateDTO);
        service.AddNew(attributeTemplateDTO);
        log.debug("添加属性模板成功！");
        return JsonResult.ok();
    }

    @ApiOperation("查询属性模板列表")
    @ApiOperationSupport(order = 400)
    @GetMapping ("")
    public JsonResult<List<AttributeTemplateListItemVO>> list(){
        log.debug("开始处理查询属性模板列表的业务");
        List<AttributeTemplateListItemVO> list = service.list();
        return JsonResult.ok(list);
    }

    @ApiOperation("删除属性模板")
    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id",value = "相册ID",required = true,dataType = "long")
    public JsonResult deleteById(@PathVariable @Range(min = 1,message = "请提交有效的ID值！")Long id){
        service.deleteById(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改属性模板")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "属性模板ID", required = true, dataType = "long")
    })
    public JsonResult<Void> updateInfoById(@PathVariable @Range(min = 1, message = "请提交有效的ID值！") Long id, @Valid AttributeTemplateUpdateDTO attributeTemplateUpdateDTO) {
        log.debug("开始处理【修改属性模板】的请求，ID：{}，新数据：{}", id, attributeTemplateUpdateDTO);
        service.updateInfoById(id, attributeTemplateUpdateDTO);
        return JsonResult.ok();
    }

    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据ID查询属性模板详情")
    @ApiOperationSupport(order = 410)
    public JsonResult<AttributeTemplateStandardVO> getStandardById(
            @PathVariable @Range(min = 1, message = "请提交有效的ID值！") Long id) {
        log.debug("开始处理【根据ID查询属性模板详情】的请求，参数：{}", id);
        AttributeTemplateStandardVO standardById = service.getStandardById(id);
        return JsonResult.ok(standardById);
    }

}
